package com.xq.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import java.nio.charset.StandardCharsets;

/**
 * 功能说明:生产者类.
 * <p>创建时间:2018-12-31 10:15</p>
 *
 * @author <a href="https://gitee.com/gkq/microservice">郭凯强</a>
 * @since 1.0.0
 */
public class Send {
    private static final String QUEUE_NAME = "hello";
    private static final Logger logger = LoggerFactory.getLogger(Send.class);
    private static final Marker marker = MarkerFactory.getMarker("Send");

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.1.77");
        factory.setUsername("admin");
        factory.setPassword("gkq1986");

        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);

            String message = "Hello World!";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
            logger.debug(marker, "[x] Sent {}", message);
        }
    }
}
